package com.fjh.springboot_01.mapper;

import com.fjh.springboot_01.pojo.payment.PaymentOrder;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface PaymentOrderMapper {
    
    @Insert("INSERT INTO payment_order(order_no, booking_id, tutoring_id, student_id, teacher_id, amount, status, payment_method, create_time, update_time) " +
            "VALUES(#{orderNo}, #{bookingId}, #{tutoringId}, #{studentId}, #{teacherId}, #{amount}, #{status}, #{paymentMethod}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PaymentOrder paymentOrder);
    
    @Select("SELECT * FROM payment_order WHERE id = #{id}")
    PaymentOrder findById(Integer id);
    
    @Select("SELECT p.*, u1.username as student_name, u2.username as teacher_name, t.title as tutoring_title " +
            "FROM payment_order p " +
            "LEFT JOIN user u1 ON p.student_id = u1.id " +
            "LEFT JOIN user u2 ON p.teacher_id = u2.id " +
            "LEFT JOIN tutoring t ON p.tutoring_id = t.id " +
            "WHERE p.order_no = #{orderNo}")
    PaymentOrder findByOrderNo(String orderNo);
    
    @Select("SELECT p.*, u1.username as student_name, u2.username as teacher_name, t.title as tutoring_title " +
            "FROM payment_order p " +
            "LEFT JOIN user u1 ON p.student_id = u1.id " +
            "LEFT JOIN user u2 ON p.teacher_id = u2.id " +
            "LEFT JOIN tutoring t ON p.tutoring_id = t.id " +
            "WHERE p.booking_id = #{bookingId}")
    PaymentOrder findByBookingId(Integer bookingId);
    
    @Select("SELECT p.*, u1.username as student_name, u2.username as teacher_name, t.title as tutoring_title " +
            "FROM payment_order p " +
            "LEFT JOIN user u1 ON p.student_id = u1.id " +
            "LEFT JOIN user u2 ON p.teacher_id = u2.id " +
            "LEFT JOIN tutoring t ON p.tutoring_id = t.id " +
            "WHERE p.student_id = #{studentId} " +
            "ORDER BY p.create_time DESC")
    List<PaymentOrder> findByStudentId(Integer studentId);
    
    @Select("SELECT p.*, u1.username as student_name, u2.username as teacher_name, t.title as tutoring_title " +
            "FROM payment_order p " +
            "LEFT JOIN user u1 ON p.student_id = u1.id " +
            "LEFT JOIN user u2 ON p.teacher_id = u2.id " +
            "LEFT JOIN tutoring t ON p.tutoring_id = t.id " +
            "WHERE p.teacher_id = #{teacherId} " +
            "ORDER BY p.create_time DESC")
    List<PaymentOrder> findByTeacherId(Integer teacherId);
    
    @Update("UPDATE payment_order SET status = #{status}, trade_no = #{tradeNo}, payment_time = #{paymentTime}, update_time = #{updateTime} WHERE order_no = #{orderNo}")
    int updatePaymentStatus(@Param("orderNo") String orderNo, @Param("status") Integer status, @Param("tradeNo") String tradeNo, @Param("paymentTime") LocalDateTime paymentTime, @Param("updateTime") LocalDateTime updateTime);
} 